#include "StdAfx.h"
#include "SipEveryHandler.h"
#include "ResipTesterDlg.h"
#include "DatabaseAdaptor.h"

CSipRealPlayHandler::CSipRealPlayHandler(
    CResipTesterDlg& dlg, 
    CSipInviteSession& invs, 
    std::list<CSipRealPlayHandler*>& list):
m_dlg(dlg), 
m_invSession(invs), 
m_list(list)
{
    m_invSession.registerHandle(this);
    m_list.push_back(this);
}

CSipRealPlayHandler::~CSipRealPlayHandler(void)
{
    m_list.remove(this);
}


void CSipRealPlayHandler::start(std::string& body){
    m_invSession.start(body);
}


void CSipRealPlayHandler::shutdown()
{
    //m_invSession.unregisterHandle(this);
    m_invSession.stop();
}


void CSipRealPlayHandler::ptzControl(std::string& body){
    m_invSession.ptzControl(body, 10);
}

////////////////////////////////////////////////////////////////

bool CSipRealPlayHandler::onCancel(SipMessage& msg, CSipInviteSession& invs)
{
    m_dlg.pushLogText(_T("invite Cancel.\r\n"), m_dlg.m_MonitorEdit);
    invs.replyFail(msg.getTransactionId(), CSipSessionBase::ReqEnded);
    return true;
}


bool CSipRealPlayHandler::onCanceled(SipMessage& msg, CSipInviteSession& invs){
    m_dlg.pushLogText(_T("invite Canceled.\r\n"), m_dlg.m_MonitorEdit);
    return true;
}


bool CSipRealPlayHandler::onPtzControl(SipMessage& msg, CSipInviteSession& invs)
{
    CDatabaseAdaptor* db = CDatabaseAdaptor::getInstance();
    m_dlg.pushLogText(_T("ptz control.\r\n"), m_dlg.m_MonitorEdit);
    if (msg.getContents())
    {
        std::string body(msg.getContents()->getBodyData().c_str());
        m_dlg.pushLogText(body, m_dlg.m_BodyMonitor);
    }

    if (db->canForceFail())
    {
        invs.replyFail(msg.getTransactionId(), CSipSessionBase::Rejected);
    } else 
    {
        invs.replyOK(msg.getTransactionId(), std::string("Ptz OK"));
    }
    return true;
}


bool CSipRealPlayHandler::onConnected(SipMessage& msg, CSipInviteSession& invs)
{
    m_dlg.pushLogText(_T("invite Connected.\r\n"), m_dlg.m_MonitorEdit);
    if (msg.method() == resip::INVITE){
        invs.replyAck();
    }
    return true;
}


bool CSipRealPlayHandler::onDisconnected(CSipInviteSession& invs)
{
    m_dlg.pushLogText(_T("invite Disconnected.\r\n"), m_dlg.m_MonitorEdit);
    return true;
}


bool CSipRealPlayHandler::onTerminated(SipMessage& msg, CSipInviteSession& invs)
{
    m_dlg.pushLogText(_T("invite Terminated.\r\n"), m_dlg.m_MonitorEdit);
    return true;
}


bool CSipRealPlayHandler::onTerminate(SipMessage& msg, CSipInviteSession& invs)
{
    CDatabaseAdaptor* db = CDatabaseAdaptor::getInstance();
    m_dlg.pushLogText(_T("invite Terminate.\r\n"), m_dlg.m_MonitorEdit);
    std::string none;

    if (db->canForceFail())
    {
        invs.replyFail(msg.getTransactionId(), CSipSessionBase::Rejected);
    } else 
    {
        invs.replyOK(msg.getTransactionId(), none);
    }
    return true;
}


bool CSipRealPlayHandler::onSuccess(SipMessage& msg, CSipSessionBase& sb){
    if (msg.method() == resip::DO)
    {
        m_dlg.pushLogText(_T("ptz success.\r\n"), m_dlg.m_MonitorEdit);
    }
    return true;
}


bool CSipRealPlayHandler::onFailed(SipMessage& msg, CSipSessionBase& sb)
{    
    if (msg.method() == resip::INVITE)
    {
		m_dlg.pushLogText(_T("invite Failed.\r\n"), m_dlg.m_MonitorEdit);
        if (msg.getReceivedTransport() != 0){
            //m_invSession.replyFailAck();
        }
	} else if (msg.method() == resip::DO){
		m_dlg.pushLogText(_T("ptz Failed.\r\n"), m_dlg.m_MonitorEdit);
	}
    return true;
}


bool CSipRealPlayHandler::onTrying(SipMessage& msg, CSipSessionBase& sb)
{
    m_dlg.pushLogText(_T("invite trying.\r\n"), m_dlg.m_MonitorEdit);
    return true;
}


bool CSipRealPlayHandler::onDestroy(CSipSessionBase& sb)
{
    m_dlg.pushLogText(_T("invite Destroy.\r\n"), m_dlg.m_MonitorEdit);
    delete this;
    return true;
}

/////////////////////////////////////////////////////////////


CSipRegisterHandler::CSipRegisterHandler(CResipTesterDlg& dlg, 
                                         CSipRegisterSession& regs, 
                                         std::list<CSipRegisterHandler*>& list):
m_dlg(dlg), m_regSession(regs), m_list(list), m_regSuccess(false)
{
    m_regSession.registerHandle(this);
    m_list.push_back(this);
}


CSipRegisterHandler::~CSipRegisterHandler(){
    m_list.remove(this);
}


void CSipRegisterHandler::start(std::string& body){
    m_regSession.start(body);
}


void CSipRegisterHandler::stop(){
    //m_regSession.unregisterHandle(this);
    m_regSession.stop();
}


bool CSipRegisterHandler::regSuccess(){
    return m_regSuccess;
}


const resip::Uri& CSipRegisterHandler::getRemoteUri(){
    return m_regSession.getRemoteUri();
}



bool CSipRegisterHandler::onSuccess(SipMessage& msg, CSipSessionBase& sb){
    m_regSuccess = true;
    m_dlg.pushLogText(_T("Register Successed.\r\n"), m_dlg.m_MonitorEdit);
    return true;
}


bool CSipRegisterHandler::onFailed(SipMessage& msg, CSipSessionBase& sb){
    m_dlg.pushLogText(_T("Register Failed.\r\n"), m_dlg.m_MonitorEdit);
    return true;
}


bool CSipRegisterHandler::onTrying(SipMessage& msg, CSipSessionBase& sb){
    return true;
}


bool CSipRegisterHandler::onDestroy(CSipSessionBase& sb){
    m_dlg.pushLogText(_T("Register Destroy.\r\n"), m_dlg.m_MonitorEdit);
    delete this;
    return true;
}


bool CSipRegisterHandler::onDisconnected(CSipRegisterSession& regs){
    m_regSuccess = false;
    m_dlg.pushLogText(_T("Register Disconnected.\r\n"), m_dlg.m_MonitorEdit);
    return true;
}


bool CSipRegisterHandler::onRejected(SipMessage& msg, CSipRegisterSession& regs){
    m_dlg.pushLogText(_T("Register Rejected.\r\n"), m_dlg.m_MonitorEdit);
    return true;
}


/////////////////////////////////////////////////////////////////////

CSipSubscribeHandler::CSipSubscribeHandler(
                     CResipTesterDlg& dlg, 
                     CSipSubscribeSession2& subs, 
                     std::list<CSipSubscribeHandler*>& list):
m_dlg(dlg), m_subSession(subs), m_list(list)
{
    m_subSession.registerHandle(this);
    m_list.push_back(this);
}


CSipSubscribeHandler::~CSipSubscribeHandler(){
    m_list.remove(this);
}


void CSipSubscribeHandler::subscribe(std::string& body)
{
    m_subSession.subscribe(body);
}


void CSipSubscribeHandler::unsubscribe(std::string& body)
{
    m_subSession.unsubscribe(body);
}


void CSipSubscribeHandler::notify(std::string& body)
{
    m_subSession.notify(body);
}


/////////////////// handle methods ////////////////////

bool CSipSubscribeHandler::onSubscribe(SipMessage& msg, CSipSubscribeSession2& subs)
{
    return true;
}


bool CSipSubscribeHandler::onUnsubscribe(SipMessage& msg, CSipSubscribeSession2& subs)
{
    CDatabaseAdaptor* db = CDatabaseAdaptor::getInstance();
    m_dlg.pushLogText(_T("Unsubscribe coming.\r\n"), m_dlg.m_MonitorEdit);
    if (msg.getContents())
    {
        std::string body(msg.getContents()->getBodyData().c_str());
        m_dlg.pushLogText(body, m_dlg.m_BodyMonitor);
    }
    if (db->canForceFail())
    {
        subs.replyFail(msg.getTransactionId(), CSipSessionBase::Rejected);
    } else {
        std::string body("Unsubscribe OK");
        subs.replyOK(msg.getTransactionId(), body);
    }
    return true;
}


bool CSipSubscribeHandler::onNotify(SipMessage& msg, CSipSubscribeSession2& subs)
{
    CDatabaseAdaptor* db = CDatabaseAdaptor::getInstance();
    m_dlg.pushLogText(_T("Notify coming.\r\n"), m_dlg.m_MonitorEdit);
    if (msg.getContents())
    {
        std::string body(msg.getContents()->getBodyData().c_str());
        m_dlg.pushLogText(body, m_dlg.m_BodyMonitor);
    }
    if (db->canForceFail())
    {
        subs.replyFail(msg.getTransactionId(), CSipSessionBase::Rejected);
    } else {
        std::string body("Unsubscribe OK");
        subs.replyOK(msg.getTransactionId(), body);
    }
    return true;
}


bool CSipSubscribeHandler::onSuccess(SipMessage& msg, CSipSessionBase& sb){
    m_dlg.pushLogText(_T("Successed.\r\n"), m_dlg.m_MonitorEdit);
    return true;
}


bool CSipSubscribeHandler::onFailed(SipMessage& msg, CSipSessionBase& sb){
    m_dlg.pushLogText(_T("Failed.\r\n"), m_dlg.m_MonitorEdit);
    return true;
}


bool CSipSubscribeHandler::onTrying(SipMessage& msg, CSipSessionBase& sb){
    m_dlg.pushLogText(_T("Trying.\r\n"), m_dlg.m_MonitorEdit);
    return true;
}


bool CSipSubscribeHandler::onDestroy(CSipSessionBase& sb){
    m_dlg.pushLogText(_T("Destroy.\r\n"), m_dlg.m_MonitorEdit);
    delete this;
    return true;
}

//--------------------------------------------------------------------------------------


CSipSessionHandler::CSipSessionHandler(CResipTesterDlg& dlg, std::list<CSipSessionHandler*>& list):
m_dlg(dlg), m_list(list)
{}


CSipSessionHandler::~CSipSessionHandler(){
    m_list.remove(this);
}


////////////////////////////////////

bool CSipSessionHandler::onSuccess(SipMessage& msg, CSipSessionBase& sb)
{
    m_dlg.pushLogText(_T("Successed.\r\n"), m_dlg.m_MonitorEdit);
    if (msg.getContents()){
        std::string body = msg.getContents()->getBodyData().c_str();
        m_dlg.pushLogText(body, m_dlg.m_BodyMonitor);
    }
    return true;
}


bool CSipSessionHandler::onFailed(SipMessage& msg, CSipSessionBase& sb){
    m_dlg.pushLogText(_T("Failed.\r\n"), m_dlg.m_MonitorEdit);
    return true;
}


bool CSipSessionHandler::onTrying(SipMessage& msg, CSipSessionBase& sb){
    m_dlg.pushLogText(_T("Trying.\r\n"), m_dlg.m_MonitorEdit);
    return true;
}


bool CSipSessionHandler::onDestroy(CSipSessionBase& sb){
    m_dlg.pushLogText(_T("Destroy.\r\n"), m_dlg.m_MonitorEdit);
    delete this;
    return true;
}

//---------------------------------------------------------------------

CSipB2bSessionHandler::CSipB2bSessionHandler(
    CSipTranSession& callee, std::list<CSipB2bSessionHandler*>& list):
m_callee(callee), m_list(list)
{
    m_list.push_back(this);
}


CSipB2bSessionHandler::~CSipB2bSessionHandler(){
    m_list.remove(this);
}


////////////////////////////////////

bool CSipB2bSessionHandler::onSuccess(SipMessage& msg, CSipSessionBase& sb){
    if (msg.isResponse()){
        std::string body;
        if (msg.getContents()){
            body = msg.getContents()->getBodyData().c_str();
        }
        m_callee.replyOK(body);
    }
    return true;
}


bool CSipB2bSessionHandler::onFailed(SipMessage& msg, CSipSessionBase& sb){
    if (msg.isResponse()){
        int code = msg.header(h_StatusLine).responseCode();
        m_callee.replyFail(CSipSessionBase::getReasonbyCode(code));
    }
    return true;
}


bool CSipB2bSessionHandler::onTrying(SipMessage& msg, CSipSessionBase& sb){
    return true;
}


bool CSipB2bSessionHandler::onDestroy(CSipSessionBase& sb){
    delete this;
    return true;
}


